package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.CheckPhonePost;
import com.longcai.piaojia.conn.GetCodePost;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

/**
 * Created by Administrator on 2016/8/30.
 */
public class ConfirmOldPhoneActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.confirm_old_phone_submit_tv) private TextView confirm_old_phone_submit_tv;
    @BoundView(R.id.confirm_old_phone_verification_code_tv) private TextView confirm_old_phone_verification_code_tv;
    @BoundView(R.id.confirm_old_phone_verification_code_et) private TextView confirm_old_phone_verification_code_et;
    @BoundView(R.id.confirm_old_phone_phonenum_et) private TextView confirm_old_phone_phonenum_et;
    private GetCodePost.CodeEntity codeEntityConfirmOldPhone;

    private int netCode;

    private GetCodePost getCodePost = new GetCodePost(new AsyCallBack<GetCodePost.CodeEntity>(){

        @Override
        public void onSuccess(String toast, int type, GetCodePost.CodeEntity codeEntity) throws Exception {
            super.onSuccess(toast, type, codeEntity);

            codeEntityConfirmOldPhone = codeEntity;

            netCode = codeEntity.code;

            Toast.makeText(ConfirmOldPhoneActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(ConfirmOldPhoneActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });

    private CheckPhonePost checkPhonePost = new CheckPhonePost(new AsyCallBack<CheckPhonePost.CheckPhoneEntity>(){

        @Override
        public void onSuccess(String toast, int type, CheckPhonePost.CheckPhoneEntity checkPhoneEntity) throws Exception {
            super.onSuccess(toast, type, checkPhoneEntity);

            Intent intent = new Intent(ConfirmOldPhoneActivity.this, EditPhoneNumActivity.class).putExtra("change_token", checkPhoneEntity.change_token);

            startActivity(intent);

            finish();

            Toast.makeText(ConfirmOldPhoneActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(ConfirmOldPhoneActivity.this, toast, Toast.LENGTH_LONG).show();

            countTime.cancel();
            confirm_old_phone_verification_code_tv.setEnabled(true);
            confirm_old_phone_verification_code_tv.setText(getString(R.string.register_SMS_verification_code_text));

        }

    });

    private CountDownTimer countTime = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                confirm_old_phone_verification_code_tv.setText(millisUntilFinished / 1000
                        + getString(R.string.edit_phonenum_code_count_down_time_text));

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {

                confirm_old_phone_verification_code_tv.setText(getString(R.string.edit_phonenum_code_text));
                confirm_old_phone_verification_code_tv.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_confirm_old_phone);

        setBackTrue();

        setTitleName(getString(R.string.edit_phonenum_title_text));

        confirm_old_phone_verification_code_tv.setOnClickListener(this);
        confirm_old_phone_submit_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        String phone = confirm_old_phone_phonenum_et.getText().toString().trim();
        String code = confirm_old_phone_verification_code_et.getText().toString().trim();

        switch (v.getId()){

            case R.id.confirm_old_phone_verification_code_tv:

                if (TextUtils.isEmpty(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_phone_text), Toast.LENGTH_LONG);

                    return;

                } else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                }

                if (!BaseApplication.BasePreferences.readUserAccount().equals(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                }

                getCodePost.phone = phone;
                getCodePost.type = "find";
//                getCodePost.change = BaseApplication.BasePreferences.readUserId();
                getCodePost.change = "true";
                getCodePost.execute(context);

                confirm_old_phone_verification_code_tv.setEnabled(false);

                countTime.start();

                break;

            case R.id.confirm_old_phone_submit_tv:

                if (TextUtils.isEmpty(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_phone_text), Toast.LENGTH_LONG);

                    return;

                } else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                } else if (netCode == 0) {

                    MyToast.show(context, getString(R.string.warning_please_click_get_code_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(code)) {

                    MyToast.show(context, getString(R.string.register_verification_code_hint), Toast.LENGTH_LONG);

                    return;

                } else if (Integer.parseInt(code) != codeEntityConfirmOldPhone.code) {

                    MyToast.show(context, getString(R.string.warning_bad_code_text), Toast.LENGTH_LONG);

                    return;

                }

                checkPhonePost.phone = phone;
                checkPhonePost.code = code;
                checkPhonePost.user_id = BaseApplication.BasePreferences.readUserId();
                checkPhonePost.execute(ConfirmOldPhoneActivity.this);

                break;
        }

    }


}
